﻿using System;
using System.Collections.Generic;
using System.IO;
using MassSpectrum.dto;

namespace MassSpectrum.Model
{
    public class DataWriter : IDataWriter
    {
        public void WriteData(IList<Data> data, string path)
        {
            if (System.IO.File.Exists(path))
                {
                    AppendData(data, path);
                }
                else
                {
                    CreateData(data, path);
                }
        }

        private void AppendData(IList<Data> data, string path)
        {
            using (StreamWriter sw = new StreamWriter(File.Open(path, FileMode.Append)))
            {
                foreach (Data d in data)
                {
                    sw.WriteLine(d.id.ToString() + '\t' + d.Time.ToString() + '\t' + d.Mass.ToString() + '\t' + d.Intensity.ToString() + '\t' + d.Temperature.ToString() + '\t' + d.Heating.ToString());
                }
                sw.Close();
            }
        }


        private void CreateData(IList<Data> data, string path)
        {
            using (StreamWriter sw = File.CreateText(path))
            {
                foreach (Data d in data)
                {
                    sw.WriteLine(d.id.ToString() + '\t' + d.Time.ToString() + '\t' + d.Mass.ToString() + '\t' + d.Intensity.ToString() + '\t' + d.Temperature.ToString() + '\t' + d.Heating.ToString());
                }
                sw.Close();
            }       
        }


        public void WriteFile(string source, string target)
        {

            
            if (!Directory.Exists(Path.GetDirectoryName(target)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(target));
            }
                       
            File.Copy(source, target, true);
           
        }
    }
}
